package com.airport.ticket.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.airport.ticket.entity.UserInfo;

/**
 * 登录过滤器
 *
 */
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {}

    @Override
    public void destroy() {}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;
	
        HttpSession session = httpRequest.getSession();
        
        try {
        	/**
        	 * 判断用户是否登录
        	 * 如果没有登录则返回登录页
        	 */
        	UserInfo loginUserInfo = (UserInfo)session.getAttribute("loginUserInfo");
            if(loginUserInfo==null || loginUserInfo.getId()==null || "".equals(loginUserInfo.getId())) {
            	httpResponse.sendRedirect("/login.html");
            }
        }catch (Exception e) {
        	e.printStackTrace();
        	httpResponse.sendRedirect("/login.html");
		}


        chain.doFilter(request, response);
        
	}
	
	
	
}
